<template lang="html">
    <div class="container">
        <div class="search">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                  <el-input class="form_input" v-model="searchText" placeholder="请输入店铺名称"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" :disabled="loading" size="small" @click="onSubmit">搜索</el-button>
                  <el-button type="primary" size="small" @click="onAddtion">新增</el-button>
                  <el-button type="danger" :disabled="isDisabled" size="small" @click="onBatch">批量删除</el-button>
                </el-form-item>
                <el-form-item>
                  <div class="f_view">
                    <el-date-picker v-model="date" type="daterange" @change="onDate" value-format="yyyy-MM-dd"
                    range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
                    <div class="buttons">
                        <el-button size="small" @click="filter(0)" :class="[active==0?'active':'']" type="primary" plain>月</el-button>
                        <el-date-picker
                          class="date_picker"
                          value-format="yyyy-MM"
                          ref="date_picker_month"
                          @change="onMonth"
                          v-model="month"
                          type="month">
                        </el-date-picker>
                    </div>
                    <div class="buttons">
                        <el-button size="small" @click="filter(1)" :class="[active==1?'active':'']" type="primary" plain>年</el-button>
                        <el-date-picker
                          class="date_picker"
                          value-format="yyyy"
                          ref="date_picker_year"
                          @change="onYear"
                          v-model="year"
                          type="year">
                        </el-date-picker>
                    </div>
                    &nbsp;
                    &nbsp;
                    &nbsp;
                    &nbsp;
                    <span>统计时间：{{statisTime}}</span>

                  </div>
                </el-form-item>

            </el-form>
        </div>

        <div class="tables" v-loading="loading">
          <el-table :data="tableData" :row-class-name="rowStyle" @selection-change="handleSelectionChange" border :header-cell-style="{background:'#f5f5f5',color:'#666'}" style="width: 100%">
                <el-table-column type="selection" width="40"></el-table-column>
                <el-table-column prop="mainStoreName" label="店铺名称" align="center"></el-table-column>
                <el-table-column prop="sumCount" label="客户" align="center"></el-table-column>
                <el-table-column prop="sumPrice" label="业绩" align="center"></el-table-column>
                <el-table-column prop="userName" label="管理员" align="center"></el-table-column>
                <el-table-column label="状态" align="center" width="100">
                  <template slot-scope="scope">
                      <el-switch @change="onSwitch(scope.row)" v-model="scope.row.opState" :active-value="1" :inactive-value="2" active-color="#4196FF" inactive-color="#D7D7D7"></el-switch>
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="300">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="fEdit(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="fDelete(scope.row)">删除</el-button>
                        <el-button type="warning" size="mini" @click="fDetail(scope.row)">详情</el-button>
                    </template>
                </el-table-column>
          </el-table>
          <pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :currentPage="currentPage" :totalPage="totalPage" />
        </div>



    </div>
</template>

<script>
export default {
  data(){
    return {
      loading: false,
      searchText: '',
      statisTime: '',
      currentPage: 1,
      totalPage: 0,
      pageSize:10,
      tableData: [],
      currentData:[],
      date: [],
      month: '',
      year: '',
      active: null,
    }
  },
  computed:{
    isDisabled:function() {
      return this.currentData.length>0?false:true;
    }
  },
  created(){
    this.selectStorePage();

  },
  methods:{
    rowStyle({row,rowIndex}) {
      for(let i=0;i<this.currentData.length;i++){
        if(JSON.stringify(row) == JSON.stringify(this.currentData[i])){
          return 'row_style';
        }
      }
    },
    handleSelectionChange(e){
      this.currentData = [];
      this.currentData = this.currentData.concat(e);
    },
    selectStorePage(){
      this.loading = true;
      this.$http.selectStorePage({
        query: this.searchText,
        startTime: (this.date&&this.date[0]) || '',
        endTime: (this.date&&this.date[1]) || '',
        monthTime: this.month,
        yearTime: this.year,
        pageNumber: this.currentPage,
        pageSize:this.pageSize,
      }).then(res=>{
          this.loading = false;
          this.tableData = res.data.rows;
          this.totalPage = res.data.total;
          this.pageSize = res.data.pageSize;
      });
    },
    onBatch(){
      let aId = [];
      this.currentData.forEach(item=>{aId.push(item.id)});
      this.$confirm('您确定要删除这些账号吗？').then(res=>{
        return this.$http.deleteStores({ids:aId.join(',')});
      }).then(res=>{
        if(res.data.code == 0){
          this.$message.success('删除成功');
          this.selectStorePage();
        }else{
          this.$message.error(res.data.message);
        }
      }).catch(e=>0);
    },
    onSubmit(){
      this.handleCurrentChange(1);
    },
    onDate(){
      this.active = null;
      this.year = '';
      this.month = '';
      this.statisTime = `${this.date[0]} - ${this.date[1]}`;
      this.handleCurrentChange(1);
    },
    onMonth(){
      this.year = '';
      this.date = [];
      this.statisTime = this.month;
      this.handleCurrentChange(1);
    },
    onYear(){
      this.month = '';
      this.date = [];
      this.statisTime = this.year;
      this.handleCurrentChange(1);
    },
    filter(n){
      this.active = n;
      switch (this.active) {
        case 0:
          this.$refs.date_picker_month.focus();
          break;
        case 1:
          this.$refs.date_picker_year.focus();
          break;
      }
    },
    onSwitch(e){
      this.$http.closeStore({id: e.id,opState:e.opState}).then(res=>{
        if(res.data.code===0){
            this.$message.success(res.data.message);
        }else{
            this.$message.error(res.data.message);
        }
      })
    },
    onAddtion(){
      this.$router.push({
        path: '/shop/list/add_head_office',
        query: {
          title: '新增总店'
        }
      });
    },
    fEdit(e){
      this.$router.push({path: '/shop/list/add_head_office',query: {title: '编辑总店',id:e.id}});
    },
    fDelete(e){
      this.$confirm('您确定要删除这个店铺吗？').then(res=>{
         this.$http.deleteStore({id:e.id}).then(res=>{
             if(res.data.code == 0){
               this.$message.success('删除成功');
               this.selectStorePage();
             }else{
               this.$message.error(res.data.message);
             }
         })
      }).catch(e=>0);
    },
    fDetail(e){
      this.$router.push({
        path: '/shop/list/head_office_detail',
        query:{id: e.id}
      })
    },
    handleSizeChange(e){
      this.pageSize = e;
      this.handleCurrentChange(1);
    },
    handleCurrentChange(e){
      this.currentPage = e;
      this.selectStorePage();
    },



  }

}
</script>

<style lang="css" scoped>
.active{
  color: #FFF;
  background-color: #409EFF;
  border-color: #409EFF;
}
/deep/.el-table .row_style{
  background-color: #f8f8f8 !important;
}
.f_view{
  display: flex;
}
.f_view .buttons{
  margin-left: 20px;
  position: relative;
}
.buttons .date_picker{
  position: absolute;
  width: 20px;
  height: 32px;
  left: 0;
  top: 0;
  z-index: -9
}
</style>
